package models

import (
	_ "time"

	orm "go-papers/database"
	"go-papers/tools"
)

type ThirdUser struct {
	ThridUserId    int    `json:"thridUserId" gorm:"type:int(11) unsigned;primary_key"` //
	ThridOpenId    string `json:"thridOpenId" gorm:"type:varchar(255);"`                // 第三方 关联openid 开放id
	ThirdType      string `json:"thirdType" gorm:"type:varchar(32);"`                   // 第三方 关联类型 wx  ali weibo  等
	ThridNickName  string `json:"thridNickName" gorm:"type:text;"`                      // 昵称
	ThridAvatar    string `json:"thridAvatar" gorm:"type:varchar(255);"`                // 头像地址
	ThirdSex       string `json:"thirdSex" gorm:"type:varchar(255);"`                   // 性别
	ThirdSubscribe string `json:"thirdSubscribe" gorm:"type:varchar(255);"`             // 是否订阅 1 是
	ThirdUnionId   string `json:"thirdUnionId" gorm:"type:varchar(255);"`               // 公共id
	ThirdAccountId string `json:"thirdAccountId" gorm:"type:varchar(255);"`             // 第三方账户
	ThirdExtra     string `json:"thirdExtra" gorm:"type:text;"`                         // 额外信息
	ThirdAddr      string `json:"thirdAddr" gorm:"type:varchar(255);"`                  // 所在地
	ThirdStatus    string `json:"thirdStatus" gorm:"type:varchar(8);"`                  // 状态 1 默认， 0 无效
	ThirdActive    string `json:"thirdActive" gorm:"type:varchar(255);"`                // 激活时间 活跃点
	DataScope      string `json:"dataScope" gorm:"-"`
	Params         string `json:"params"  gorm:"-"`
	Token          string `json:"token"  gorm:"-"`

	BaseModel
}

func (ThirdUser) TableName() string {
	return "third_user"
}

// 创建ThirdUser
func (e *ThirdUser) Create() (ThirdUser, error) {
	var doc ThirdUser
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// GetIDByOpenId
func (e *ThirdUser) GetIDByOpenId(openId string) (int, error) {
	var doc ThirdUser
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("thrid_open_id = ?", openId)
	if err := table.First(&doc).Error; err != nil {
		return doc.ThridUserId, err
	}
	return 0, nil
}

// 获取ThirdUser
func (e *ThirdUser) Get() (ThirdUser, error) {
	var doc ThirdUser
	table := orm.Eloquent.Table(e.TableName())
	if e.ThridUserId != 0 {
		table = table.Where("thrid_user_id = ?", e.ThridUserId)
	}
	if e.ThridOpenId != "" {
		table = table.Where("thrid_open_id = ?", e.ThridOpenId)
	}
	if e.ThirdType != "" {
		table = table.Where("third_type = ?", e.ThirdType)
	}
	if e.ThridNickName != "" {
		table = table.Where("thrid_nick_name = ?", e.ThridNickName)
	}
	if e.ThridAvatar != "" {
		table = table.Where("thrid_avatar = ?", e.ThridAvatar)
	}
	if e.ThirdSex != "" {
		table = table.Where("third_sex = ?", e.ThirdSex)
	}
	if e.ThirdUnionId != "" {
		table = table.Where("third_union_id = ?", e.ThirdUnionId)
	}
	if e.ThirdAccountId != "" {
		table = table.Where("third_account_id = ?", e.ThirdAccountId)
	}
	if e.ThirdExtra != "" {
		table = table.Where("third_extra = ?", e.ThirdExtra)
	}
	if e.ThirdAddr != "" {
		table = table.Where("third_addr = ?", e.ThirdAddr)
	}
	if e.ThirdStatus != "" {
		table = table.Where("third_status = ?", e.ThirdStatus)
	}
	if e.ThirdActive != "" {
		table = table.Where("third_active = ?", e.ThirdActive)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取ThirdUser带分页
func (e *ThirdUser) GetPage(pageSize int, pageIndex int) ([]ThirdUser, int, error) {
	var doc []ThirdUser

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.ThridUserId != 0 {
		table = table.Where("thrid_user_id = ?", e.ThridUserId)
	}
	if e.ThridOpenId != "" {
		table = table.Where("thrid_open_id = ?", e.ThridOpenId)
	}
	if e.ThirdType != "" {
		table = table.Where("third_type = ?", e.ThirdType)
	}
	if e.ThridNickName != "" {
		table = table.Where("thrid_nick_name = ?", e.ThridNickName)
	}
	if e.ThridAvatar != "" {
		table = table.Where("thrid_avatar = ?", e.ThridAvatar)
	}
	if e.ThirdSex != "" {
		table = table.Where("third_sex = ?", e.ThirdSex)
	}
	if e.ThirdUnionId != "" {
		table = table.Where("third_union_id = ?", e.ThirdUnionId)
	}
	if e.ThirdAccountId != "" {
		table = table.Where("third_account_id = ?", e.ThirdAccountId)
	}
	if e.ThirdExtra != "" {
		table = table.Where("third_extra = ?", e.ThirdExtra)
	}
	if e.ThirdAddr != "" {
		table = table.Where("third_addr = ?", e.ThirdAddr)
	}
	if e.ThirdStatus != "" {
		table = table.Where("third_status = ?", e.ThirdStatus)
	}
	if e.ThirdActive != "" {
		table = table.Where("third_active = ?", e.ThirdActive)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新ThirdUser
func (e *ThirdUser) Update(id int) (update ThirdUser, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("thrid_user_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

//InsertUpdate
func (e *ThirdUser) InsertUpdate() (update ThirdUser, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("third_type = ? and thrid_open_id = ? ", e.ThirdType, e.ThridOpenId).First(&update).Error; err != nil {
		//记录不存在  insert
		var doc ThirdUser
		result := orm.Eloquent.Table(e.TableName()).Create(&e)
		if result.Error != nil {
			err := result.Error
			return doc, err
		}
		doc = *e
		return doc, nil
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除ThirdUser
func (e *ThirdUser) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("thrid_user_id = ?", id).Delete(&ThirdUser{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ThirdUser) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("thrid_user_id in (?)", id).Delete(&ThirdUser{}).Error; err != nil {
		return
	}
	Result = true
	return
}
